import logging

import pytest

from resource.pom.launcher import Launcher
from utils.manager import Images


class Test:
    launcher = Launcher

    @pytest.mark.parametrize('setup_teardown', [None], indirect=True)
    def test(self, setup_teardown, device):
        for i in range(5):
            logging.info('步骤1.{}：长按桌面空白处'.format(i))
            device.long_click(360, 640)
            logging.info('预期结果1.{}：出现选择桌面编辑项弹窗，无卡死、黑屏、白屏等现象'.format(i))
            device.refresh_layout()
            device.assert_text_exist('选择桌面编辑项')
            device.assert_text_exist('取消')
            device.assert_text_exist('添加空白页')
            device.assert_text_exist('更多设置')
            dialog_area = (18, 1036, 701, 1219)
            dialog_snapshot = device.save_snapshot_to_local('{}.jpeg'.format(i))
            Images.crop_picture(dialog_snapshot, *dialog_area)
            similarity = Images.compare_image_similarity(dialog_snapshot, self.launcher.desktop_dialog)
            device.click_element(device.get_element_by_text('取消'))
            assert similarity > 0.6, '桌面编辑弹窗和标准图相似度低于80%，请查看测试截图'
            device.wait(1)
